import React, { Component } from 'react'
import "../assert/css/goodsdetail.css"
import qs from 'qs'
import Header from "../components/DefaultHeader"
import {cartAdd} from '../request/cart_request'
export default class GoodsDetail extends Component {
    state={
        goods:{},
        id:{},
    }
    get_goods(id){
        this.$axios.get('/getgoodsinfo?id='+id).then(res=>{
            this.setState({goods:res.list[0]})
        })
    }
    UNSAFE_componentWillMount(){

        const {id}= qs.parse(this.props.location.search.substring(1))
        this.get_goods(id)
        this.setState({id:id})
        
    }
    addCar(goodsid,type=2){
        //type确定是万人团，是2
        //num确定为1
        cartAdd(goodsid,type,1).then(res=>{
           if(res.code == 200){
            this.props.restCarList()
           }
        })

    }
    render() {
        
        const {goods,id} = this.state
        return (
            <div className="goods-container">
            <Header title="商品详情"/>
            <div className="detail">
                <img src={this.$static_host+goods.img} alt=""/>
                <div>
                    <h3 className="price">&yen;{goods.price}</h3>
                    <p>{goods.goodsname}</p>
                </div>
            </div>
            <ul className="cartbar">
                <li>
                    <div className="icon icon-concat">客服</div>
                </li>
                <li>
                    <div className="icon icon-shop">店铺</div>
                </li>
                <li>
                    <div className="icon icon-cart">购物车</div>
                </li>
                <li className="btn-cart">
                    加入购物车
                </li>
                <li className="btn-buy"  onClick={()=>this.addCar(id,1)}>
                    立即购买
                </li>
            </ul>
        </div>

        )
    }
}
